package controllers

import (
	"Mountian/btc"
	"Mountian/models"

	"github.com/astaxie/beego"
)

type AddressController struct {
	beego.Controller
}

//检查地址属性
// @router /check [get]
func (c *AddressController) Check() {

	var ret models.Response
	var add string
	c.Ctx.Input.Bind(&add, "add")

	if add[:2] == "0x" {
		ret.Data = "ETH"
		if len(add) == 42 {
			ret.Code = 1
		} else {
			ret.Code = 0
			ret.Msg = "错误的ETH地址"
		}
	} else if (add[:1] == "1" || add[:1] == "3") && len(add) == 34 {
		ret.Data = "BTC"

		ok, Type, err := btc.ValidateAddress([]byte(add))

		if ok && (Type == 0 || Type == 5) && err == nil {
			ret.Code = 1
		} else {
			ret.Code = 0
			ret.Msg = "错误的BTC地址"
		}
	} else {
		ret.Code = 0
		ret.Msg = "无法识别的地址"
		ret.Data = ""
	}
	beego.Debug(ret, "add="+add)

	c.Data["json"] = ret
	c.ServeJSON()
}
